﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace OrderingPlatform.MvcExtension
{
    public static class HtmlHelperExtension
    {
        public static string CurrentAction(this HtmlHelper html)
        {
            return (string)html.ViewContext.RouteData.Values["action"];
        }

        public static string CurrentController(this HtmlHelper html)
        {
            return (string)html.ViewContext.RouteData.Values["controller"];
        }

        public static string CurrentArea(this HtmlHelper html)
        {
            return (string)html.ViewContext.RouteData.DataTokens["area"];
        }

        public static MvcHtmlString SelectOptions(this HtmlHelper helper, IEnumerable<string> list, string selectedValue, bool emptyOption = false)
        {
            return SelectOptionsWrap(list, selectedValue, emptyOption);
        }

        public static MvcHtmlString SelectOptions<T1, T2>(this HtmlHelper helper, IEnumerable<KeyValuePair<T1, T2>> list, string selectedValue, bool emptyOption = false)
        {
            return SelectOptions(list, selectedValue, emptyOption);
        }



        public static MvcHtmlString DatepickerFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression = null, string width = "")
        {
            var model = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
            if (string.IsNullOrEmpty(width))
            {
                width = "w110";
            }
            if (model is DateTime)
            {

                var name = ExpressionHelper.GetExpressionText(expression);
                var value = ((DateTime?)model).FormatAvoidDefault();
                var r = helper.TextBox(name, value,
                    new { @ref = "datepicker", @class = "typeDate " + width, style = "width:75px", value });
                return r;
            }

            return helper.TextBox(ExpressionHelper.GetExpressionText(expression),
                model,
                new { @ref = "datepicker", @class = "typeDate " + width, style = "width:75px" });
        }


        public static MvcHtmlString SelectOptionsWrap(IEnumerable<string> list, string selectedValue, bool emptyOption = false, string defaultString = "")
        {
            var html = new StringBuilder();
            if (emptyOption)
            {
                html.AppendFormat("<option>{0}</option>",defaultString);
            }
            if (list != null)
            {
                foreach (var option in list)
                {
                    html.AppendFormat(@"<option value=""{0}"" {1}>{0}</option>", option, option.Equals(selectedValue, StringComparison.OrdinalIgnoreCase) ? "selected=selected" : string.Empty);
                }
            }
            return MvcHtmlString.Create(html.ToString());
        }

        public static MvcHtmlString SelectOptions<T1, T2>(IEnumerable<KeyValuePair<T1, T2>> list, string selectedValue, bool emptyOption = false,string defaultString="")
        {
            var html = new StringBuilder();
            if (emptyOption)
            {
                html.AppendFormat("<option>{0}</option>", defaultString);
            }
            if (list != null)
            {
                foreach (var option in list)
                {
                    html.AppendFormat(@"<option value=""{0}"" {2}>{1}</option>", option.Value, option.Key, option.Value.ToString().Equals(selectedValue, StringComparison.OrdinalIgnoreCase) ? "selected=selected" : string.Empty);
                }
            }
            return MvcHtmlString.Create(html.ToString());
        }
    }
}
